<?php

namespace App\Server\Util;

class CurlImageUpload
{
    public function CURLPOST($Tmpfilename, $minetype, $Newfilename, $ImageFile, $CURLURL)
    {

        $postData['file_keep_name']     = $Newfilename;
        $postData['file_folder']        = $ImageFile;
        $postData['file_name']          = curl_file_create($Tmpfilename, $minetype, $Newfilename);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $CURLURL);
        //curl结果不直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //发送post  请求
        curl_setopt($ch, CURLOPT_POST, 1);
        // urlencoded 后的字符串，类似'para1=val1&para2=val2&...'，也可以使用一个以字段名为键值，字段数据为值的数组 ,测试当 值为数组时候  Content-Type头将会被设置成multipart/form-data 否则Content-Type 头会设置为 application/x-www-form-urlencoded
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));

        //允许 cURL 函数执行的最长秒数
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        //不输出header 头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //不验证证书 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 检查证书中是否设置域名,0不验证 0：不检查通用名称（CN）属性
        // 1：检查通用名称属性是否存在
        // 2：检查通用名称是否存在，是否与服务器的主机名称匹配
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        //设置 在HTTP请求中包含一个"User-Agent: "头的字符串
        //curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, [
        //     "Content-Type: multipart/form-data; boundary=" . static::$delimiter,
        //     "Content-Length: " . strlen($post_data)
        // ]);

        $res = curl_exec($ch);
        $error_no = curl_errno($ch);
        $info = curl_getinfo($ch);

        // return $error_no;
        // return $info;

        $err_msg = '';
        if ($error_no) {
            $err_msg = curl_error($ch);
        } else {
            // print_r($res);
            //dump($info);
        }
        curl_close($ch);
        return $res;
    }
}
